#include "VolumeRenderPCH.h"
#include "TFObj.h"

//**************************************************************************************************
//FUNCTION:
void CTFObj::initV()
{
	m_pTransferFuncTex = __createTransferFuncTex2D(ResourceManager::getSharedDataByName<size_t>("TFTexExtent"));
	genTexture(m_pTransferFuncTex);
}

//**************************************************************************************************
//FUNCTION:
void CTFObj::updateV()
{
	if (ResourceManager::getSharedDataByName<bool>("IsTFChanged"))
	{
		__updateTransferFuncTex2D();
		ResourceManager::updateSharedDataByName("IsTFChanged", false);
	}
}

//**************************************************************************************************
//FUNCTION:
std::shared_ptr<STexture> CTFObj::__createTransferFuncTex2D(size_t vExtent)
{
	m_pTransferFunc->init(vExtent);
	m_pTransferFunc->update(ResourceManager::getSharedDataByName<int>("MinIntensity"),
		ResourceManager::getSharedDataByName<int>("MaxIntensity"),
		ResourceManager::getSharedDataByName<int>("MinGradient"),
		ResourceManager::getSharedDataByName<int>("MaxGradient"));

	auto pTransferFunTex = std::make_shared<STexture>();
	pTransferFunTex->InternalFormat = GL_RGBA8;
	pTransferFunTex->Width = m_pTransferFunc->getExtent();
	pTransferFunTex->Height = m_pTransferFunc->getExtent();
	pTransferFunTex->ExternalFormat = GL_RGBA;
	pTransferFunTex->DataType = GL_FLOAT;
	pTransferFunTex->pDataSet.push_back(m_pTransferFunc->getPtrOfColorSet());
	pTransferFunTex->Type4MinFilter = GL_NEAREST;
	pTransferFunTex->Type4MagFilter = GL_NEAREST;
	pTransferFunTex->isMipmap = GL_FALSE;

	pTransferFunTex->XExtent = m_pTransferFunc->getExtent();
	pTransferFunTex->YExtent = m_pTransferFunc->getExtent();

	return pTransferFunTex;
}

//**************************************************************************************************
//FUNCTION:
void CTFObj::__updateTransferFuncTex2D()
{
	m_pTransferFunc->update(ResourceManager::getSharedDataByName<int>("MinIntensity"),
		ResourceManager::getSharedDataByName<int>("MaxIntensity"),
		ResourceManager::getSharedDataByName<int>("MinGradient"),
		ResourceManager::getSharedDataByName<int>("MaxGradient"));

	updateTexture(m_pTransferFuncTex);
}